package com.panfeng.xcloud.boss.provider.member.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.LocalDate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xiaobo
 * springboot1.x升级到springboot2.x时
 * 将WebMvcConfigurerAdapter替换成WebMvcConfigurer
 * @date 2018-02-07
 */
@Configuration
@EnableSwagger2
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2Config implements WebMvcConfigurer {

	@Bean
	public Docket createRestApi() {
		List<Parameter> pars = new ArrayList<>();
		ParameterBuilder tokenParam = new ParameterBuilder();
		tokenParam.name("Authorization").description("token")
				.modelRef(new ModelRef("string")).parameterType("header")
				.required(false).build();
		pars.add(tokenParam.build());
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.directModelSubstitute(Date.class, Double.class)
                .directModelSubstitute(LocalDate.class, Double.class)
                .directModelSubstitute(Byte.class, Integer.class)
				.useDefaultResponseMessages(false)
                .globalOperationParameters(pars)
                .ignoredParameterTypes(ApiIgnore.class)
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.panfeng.xcloud"))
				.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
				.apis(RequestHandlerSelectors
						.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("QuantBot of boss-member APIs")
				.description("QuantBot-面向H5、APP-用户中心（boss）-APIs接口文档说明")
				.contact(
						new Contact("xiaobo",
								"http://192.168.18.15:81/index.php/",
								"xiaoboliyaqing@163.com")).version("1.0")
				.build();
	}

	@SuppressWarnings("deprecation")
	@Bean
	UiConfiguration uiConfig() {
		return new UiConfiguration(null, "list", "alpha", "schema",
				UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("swagger-ui.html")
				.addResourceLocations(
						"classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars*")
				.addResourceLocations(
						"classpath:/META-INF/resources/webjars/");
	}
}
